/** <a href="http://www.cpupk.com/decompiler">Eclipse Class Decompiler</a> plugin, Copyright (c) 2017 Chen Chao. **/
package com.minierp.controller;

import com.minierp.bean.AlloBillItemBean;
import com.minierp.bean.QueryParameterBean;
import com.minierp.bean.QueryResult;
import com.minierp.bean.UserBean;
import com.minierp.dao.AlloBillDao;
import com.minierp.dao.AlloBillItemDao;
import com.minierp.dao.CodeRuleDao;
import com.minierp.dao.IdGenerator;
import com.minierp.dao.ProductDao;
import com.minierp.dao.QueryDao;
import com.minierp.dao.StockBalanceDao;
import com.minierp.dao.StockDao;
import com.minierp.dao.StorehouseDao;
import com.minierp.dao.TallySetDao;
import com.minierp.err.StockNoEnoughException;
import com.minierp.helper.EncAlloBillMACtrHelper;
import com.minierp.helper.EncCommonHelper;
import com.minierp.helper.PageHelper;
import com.minierp.model.AlloBill;
import com.minierp.model.AlloBillItem;
import com.minierp.model.Product;
import com.minierp.model.Stock;
import com.minierp.model.TallySet;
import com.minierp.service.AlloBillService;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;
import org.springframework.dao.CannotAcquireLockException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

public class AlloBillMACtr extends MultiActionController {
	private AlloBillDao alloBillDao;
	private AlloBillItemDao alloBillItemDao;
	private ProductDao productDao;
	private StorehouseDao storehouseDao;
	private StockDao stockDao;
	private StockBalanceDao stockBalanceDao;
	private TallySetDao tallySetDao;
	private QueryDao queryDao;
	private CodeRuleDao codeRuleDao;
	private IdGenerator idGenerator;
	private AlloBillService alloBillService;
	private DataSource dataSource;
	private String dialogSuccessView;
	private String listForExamineView;
	private String infoForExamineView;
	private String listForUnExamineView;
	private String infoForUnExamineView;
	private String listForCancelView;
	private String infoForCancelView;
	private String searchResultView;
	private String printView;

	public String getSearchResultView() {
		return this.searchResultView;
	}

	public void setSearchResultView(String searchResultView) {
		this.searchResultView = searchResultView;
	}

	public String getDialogSuccessView() {
		return this.dialogSuccessView;
	}

	public void setDialogSuccessView(String dialogSuccessView) {
		this.dialogSuccessView = dialogSuccessView;
	}

	public String getInfoForCancelView() {
		return this.infoForCancelView;
	}

	public void setInfoForCancelView(String infoForCancelView) {
		this.infoForCancelView = infoForCancelView;
	}

	public String getInfoForExamineView() {
		return this.infoForExamineView;
	}

	public void setInfoForExamineView(String infoForExamineView) {
		this.infoForExamineView = infoForExamineView;
	}

	public String getInfoForUnExamineView() {
		return this.infoForUnExamineView;
	}

	public void setInfoForUnExamineView(String infoForUnExamineView) {
		this.infoForUnExamineView = infoForUnExamineView;
	}

	public String getListForCancelView() {
		return this.listForCancelView;
	}

	public void setListForCancelView(String listForCancelView) {
		this.listForCancelView = listForCancelView;
	}

	public String getListForExamineView() {
		return this.listForExamineView;
	}

	public void setListForExamineView(String listForExamineView) {
		this.listForExamineView = listForExamineView;
	}

	public String getListForUnExamineView() {
		return this.listForUnExamineView;
	}

	public void setListForUnExamineView(String listForUnExamineView) {
		this.listForUnExamineView = listForUnExamineView;
	}

	public ProductDao getProductDao() {
		return this.productDao;
	}

	public void setProductDao(ProductDao productDao) {
		this.productDao = productDao;
	}

	public ModelAndView handleListForExamine(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		ModelAndView mv = new ModelAndView(this.listForExamineView);
		String searchKey = request.getParameter("search_key");
		String tsId = request.getParameter("ts_id");
		UserBean ub = EncCommonHelper.getCurrentUserAccount(request);
		if (tsId == null || tsId.equals("")) {
			tsId = String.valueOf(ub.getUser().getDfTsId());
		}

		Date allowStart = null;
		if (EncCommonHelper.checkPurePrivilege(request, "NO_EARL_DATA")) {
			allowStart = EncCommonHelper.getEarlDataDate(request);
		}

		List tsList = PageHelper.getTallySetList(this.tallySetDao, ub);
		int perPageNum = EncCommonHelper.getPageSize(request);
		int total = this.alloBillDao.getListCount(searchKey, false,
				Long.parseLong(tsId), ub, allowStart);
		int pageno = EncCommonHelper.getCurrentPageIndex(request, total,
				perPageNum);
		int startIndex = (pageno - 1) * perPageNum;
		List list = this.alloBillDao.getList(searchKey, false,
				Long.parseLong(tsId), ub, allowStart, startIndex, perPageNum);
		mv.addObject("qr", EncCommonHelper.parseQueryResult(list, total,
				perPageNum, pageno));
		mv.addObject("stoList", this.storehouseDao.getList(false));
		mv.addObject("searchKey", searchKey);
		mv.addObject("tsId", tsId);
		mv.addObject("tsList", tsList);
		return mv;
	}

	public ModelAndView handleInfoForExamine(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		ModelAndView mv = new ModelAndView(this.infoForExamineView);
		String id = request.getParameter("id");
		this.getAlloBill(mv, Long.parseLong(id));
		return mv;
	}

	public ModelAndView handleListForUnExamine(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		ModelAndView mv = new ModelAndView(this.listForUnExamineView);
		String searchKey = request.getParameter("search_key");
		String tsId = request.getParameter("ts_id");
		UserBean ub = EncCommonHelper.getCurrentUserAccount(request);
		if (tsId == null || tsId.equals("")) {
			tsId = String.valueOf(ub.getUser().getDfTsId());
		}

		Date allowStart = null;
		if (EncCommonHelper.checkPurePrivilege(request, "NO_EARL_DATA")) {
			allowStart = EncCommonHelper.getEarlDataDate(request);
		}

		List tsList = PageHelper.getTallySetList(this.tallySetDao, ub);
		int perPageNum = EncCommonHelper.getPageSize(request);
		int total = this.alloBillDao.getListCount(searchKey, true,
				Long.parseLong(tsId), ub, allowStart);
		int pageno = EncCommonHelper.getCurrentPageIndex(request, total,
				perPageNum);
		int startIndex = (pageno - 1) * perPageNum;
		List list = this.alloBillDao.getList(searchKey, true,
				Long.parseLong(tsId), ub, allowStart, startIndex, perPageNum);
		mv.addObject("qr", EncCommonHelper.parseQueryResult(list, total,
				perPageNum, pageno));
		mv.addObject("stoList", this.storehouseDao.getList(false));
		mv.addObject("searchKey", searchKey);
		mv.addObject("tsId", tsId);
		mv.addObject("tsList", tsList);
		return mv;
	}

	public ModelAndView handleInfoForUnExamine(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		ModelAndView mv = new ModelAndView(this.infoForUnExamineView);
		String id = request.getParameter("id");
		this.getAlloBill(mv, Long.parseLong(id));
		return mv;
	}

	public ModelAndView handleListForCancel(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		ModelAndView mv = new ModelAndView(this.listForCancelView);
		String searchKey = request.getParameter("search_key");
		String tsId = request.getParameter("ts_id");
		UserBean ub = EncCommonHelper.getCurrentUserAccount(request);
		if (tsId == null || tsId.equals("")) {
			tsId = String.valueOf(ub.getUser().getDfTsId());
		}

		Date allowStart = null;
		if (EncCommonHelper.checkPurePrivilege(request, "NO_EARL_DATA")) {
			allowStart = EncCommonHelper.getEarlDataDate(request);
		}

		List tsList = PageHelper.getTallySetList(this.tallySetDao, ub);
		int perPageNum = EncCommonHelper.getPageSize(request);
		int total = this.alloBillDao.getListForCancelCount(searchKey,
				Long.parseLong(tsId), ub, allowStart);
		int pageno = EncCommonHelper.getCurrentPageIndex(request, total,
				perPageNum);
		int startIndex = (pageno - 1) * perPageNum;
		List list = this.alloBillDao.getListForCancel(searchKey,
				Long.parseLong(tsId), ub, allowStart, startIndex, perPageNum);
		mv.addObject("qr", EncCommonHelper.parseQueryResult(list, total,
				perPageNum, pageno));
		mv.addObject("stoList", this.storehouseDao.getList(false));
		mv.addObject("searchKey", searchKey);
		mv.addObject("tsId", tsId);
		mv.addObject("tsList", tsList);
		return mv;
	}

	public ModelAndView handleInfoForCancel(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		ModelAndView mv = new ModelAndView(this.infoForCancelView);
		String id = request.getParameter("id");
		this.getAlloBill(mv, Long.parseLong(id));
		return mv;
	}

	public ModelAndView handleExamine(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		ModelAndView mv = new ModelAndView(this.dialogSuccessView);
		UserBean ub = EncCommonHelper.getCurrentUserAccount(request);
		String idStr = request.getParameter("id_str");
		String flag = request.getParameter("flag");
		boolean examined = false;
		if (flag != null && flag.equals("1")) {
			examined = true;
		}

		String[] arr = idStr.split(";");
		String msg = "审核成功";
		byte num = 0;
		if (!examined) {
			if (!EncCommonHelper.checkPrivilege(request, "ALLO_UN_EXAM")) {
				throw new Exception("Permission deny!");
			}

			msg = "反审核成功";
			if (arr.length > 1) {
				throw new Exception("multi unexamine not supported!");
			}

			if (arr.length > 0) {
				try {
					this.alloBillService.txUnExamine(Long.parseLong(arr[0]),
							this.alloBillDao, this.alloBillItemDao,
							this.stockDao, this.stockBalanceDao,
							this.idGenerator, ub);
				} catch (StockNoEnoughException arg11) {
					msg = arg11.getMessage();
				} catch (CannotAcquireLockException arg12) {
					msg = "事务冲突故障，请重新操作。";
				}

				num = 1;
			}
		} else {
			if (!EncCommonHelper.checkPrivilege(request, "ALLO_EXAM")) {
				throw new Exception("Permission deny!");
			}

			try {
				for (int se = 0; se < arr.length; ++se) {
					this.alloBillService.txExamine(Long.parseLong(arr[se]),
							this.alloBillDao, this.alloBillItemDao,
							this.stockDao, this.stockBalanceDao,
							this.idGenerator, ub);
				}
			} catch (StockNoEnoughException arg13) {
				msg = arg13.getMessage();
			} catch (CannotAcquireLockException arg14) {
				msg = "事务冲突故障，请重新操作。";
			}

			num = 1;
		}

		mv.addObject("returnValue", String.valueOf(num));
		EncCommonHelper.setAlertMessage(request, msg);
		return mv;
	}

	public ModelAndView handleCancel(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		ModelAndView mv = new ModelAndView(this.dialogSuccessView);
		UserBean ub = EncCommonHelper.getCurrentUserAccount(request);
		if (!EncCommonHelper.checkPrivilege(request, "ALLO_CANC")) {
			throw new Exception("Permission deny!");
		} else {
			String id = request.getParameter("id");
			String reason = request.getParameter("reason");
			String msg = "";

			try {
				this.alloBillService.txCancel(Long.parseLong(id), reason,
						this.alloBillDao, this.alloBillItemDao, this.stockDao,
						this.stockBalanceDao, this.idGenerator, ub);
				msg = "作废成功";
			} catch (StockNoEnoughException arg8) {
				msg = arg8.getMessage();
			} catch (CannotAcquireLockException arg9) {
				msg = "事务冲突故障，请重新操作。";
			}

			mv.addObject("returnValue", id);
			EncCommonHelper.setAlertMessage(request, msg);
			return mv;
		}
	}

	public ModelAndView handleSearchResult(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		ModelAndView mv = new ModelAndView(this.searchResultView);
		QueryParameterBean b = EncAlloBillMACtrHelper.handleSearchResult(
				request, this.dataSource);
		QueryResult qr = this.queryDao.doQuery(b.getCurrentPage(),
				b.getPageSize(), b.getQParas(), b.getOParas(), b.getGParas(),
				b.getShortNameMap(), b.getRetObjs(), (String[]) null);
		mv.addAllObjects(b.getParameters());
		mv.addObject("qr", qr);
		mv.addObject("stoList", this.storehouseDao.getList(false));
		UserBean ub = EncCommonHelper.getCurrentUserAccount(request);
		mv.addObject("myStoList", this.storehouseDao.getList(ub));
		mv.addObject("tsList", PageHelper.getTallySetList(this.tallySetDao, ub));
		return mv;
	}

	public ModelAndView handlePrint(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		String id = request.getParameter("id");
		ModelAndView mv = new ModelAndView(this.printView);
		this.getAlloBill(mv, Long.parseLong(id));
		mv.addObject("codeRule", this.codeRuleDao.get("ALLO_BILL"));
		return mv;
	}

	private void getAlloBill(ModelAndView mv, long id) {
		AlloBill alloBill = (AlloBill) this.alloBillDao.get(AlloBill.class, id);
		if (alloBill != null) {
			List itemList = this.alloBillItemDao.getListWidthProd(alloBill
					.getId());
			Object stocList = new ArrayList();
			if (alloBill.getExamined().equals("N")) {
				stocList = this.alloBillItemDao.getStockList(alloBill.getId(),
						alloBill.getFromStoId());
			}

			ArrayList arr = new ArrayList();
			BigDecimal cost = new BigDecimal(0);
			BigDecimal zero = new BigDecimal(0);

			for (int i = 0; i < itemList.size(); ++i) {
				Object[] objs = (Object[]) itemList.get(i);
				AlloBillItemBean bean = new AlloBillItemBean();
				AlloBillItem item = (AlloBillItem) objs[0];
				Product p = (Product) objs[1];
				bean.setAlloBillItem(item);
				bean.setProductCode(p.getProductCode());
				bean.setProductName(p.getProductName());
				bean.setUnit(p.getUnit());
				if (alloBill.getExamined().equals("Y")) {
					if (item.getAmount().compareTo(new BigDecimal(0)) != 0) {
						bean.setUnitPrice(item.getCost().divide(
								item.getAmount(), 2, 4));
					}
				} else {
					for (int j = 0; j < ((List) stocList).size(); ++j) {
						Stock st = (Stock) ((List) stocList).get(j);
						if (st.getProductId() == item.getProductId()) {
							if (st.getAmount().compareTo(zero) != 0) {
								bean.setUnitPrice(st.getCost().divide(
										st.getAmount(), 2, 4));
								item.setCost(st.getCost()
										.multiply(item.getAmount())
										.divide(st.getAmount(), 2, 4));
							}
							break;
						}
					}
				}

				arr.add(bean);
				cost = cost.add(item.getCost());
			}

			mv.addObject("cost", cost);
			mv.addObject("itemList", arr);
			mv.addObject("record", alloBill);
			mv.addObject(
					"ts",
					this.alloBillDao.get(TallySet.class,
							alloBill.getTallySetId()));
		}

		mv.addObject("stoList", this.storehouseDao.getList(false));
	}

	public IdGenerator getIdGenerator() {
		return this.idGenerator;
	}

	public void setIdGenerator(IdGenerator idGenerator) {
		this.idGenerator = idGenerator;
	}

	public StockDao getStockDao() {
		return this.stockDao;
	}

	public void setStockDao(StockDao stockDao) {
		this.stockDao = stockDao;
	}

	public DataSource getDataSource() {
		return this.dataSource;
	}

	public void setDataSource(DataSource dataSource) {
		this.dataSource = dataSource;
	}

	public QueryDao getQueryDao() {
		return this.queryDao;
	}

	public void setQueryDao(QueryDao queryDao) {
		this.queryDao = queryDao;
	}

	public AlloBillDao getAlloBillDao() {
		return this.alloBillDao;
	}

	public void setAlloBillDao(AlloBillDao alloBillDao) {
		this.alloBillDao = alloBillDao;
	}

	public AlloBillItemDao getAlloBillItemDao() {
		return this.alloBillItemDao;
	}

	public void setAlloBillItemDao(AlloBillItemDao alloBillItemDao) {
		this.alloBillItemDao = alloBillItemDao;
	}

	public AlloBillService getAlloBillService() {
		return this.alloBillService;
	}

	public void setAlloBillService(AlloBillService alloBillService) {
		this.alloBillService = alloBillService;
	}

	public StockBalanceDao getStockBalanceDao() {
		return this.stockBalanceDao;
	}

	public void setStockBalanceDao(StockBalanceDao stockBalanceDao) {
		this.stockBalanceDao = stockBalanceDao;
	}

	public StorehouseDao getStorehouseDao() {
		return this.storehouseDao;
	}

	public void setStorehouseDao(StorehouseDao storehouseDao) {
		this.storehouseDao = storehouseDao;
	}

	public String getPrintView() {
		return this.printView;
	}

	public void setPrintView(String printView) {
		this.printView = printView;
	}

	public CodeRuleDao getCodeRuleDao() {
		return this.codeRuleDao;
	}

	public void setCodeRuleDao(CodeRuleDao codeRuleDao) {
		this.codeRuleDao = codeRuleDao;
	}

	public TallySetDao getTallySetDao() {
		return this.tallySetDao;
	}

	public void setTallySetDao(TallySetDao tallySetDao) {
		this.tallySetDao = tallySetDao;
	}
}